package com.example.studentinfo;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    Button btn_add,btn_find;
    static DbHelper dbHelper;
    AlertDialog.Builder builder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new DbHelper(MainActivity.this,"MyDataBase,",2);
        listView=findViewById(R.id.list);
        builder = new AlertDialog.Builder(MainActivity.this);
        btn_add=findViewById(R.id.btn_add);
        btn_find=findViewById(R.id.btn_query);
        renderListView();//从数据库中加载listview
        setlistener();//设置监听器
    }
    /**
     * 将数据库数据加载到listview控件上
     */
    private void renderListView() {
        Cursor cursor = dbHelper.queryAll("Student");//获取student表中结果到cursor游标集中
        String from[] = new String[]{"xm", "xb", "xh","bj","zy","cj"};
        int to[] = new int[]{R.id.xm,R.id.xb, R.id.xh, R.id.bj, R.id.zy, R.id.cj};
        /**
         * SimpleCursorAdapter允许你绑定一个游标的列到ListView上。
         * 需要传入当前的上下文、一个layout文件，一个游标
         * 和两个数组：一个包含使用的游标集中列的名字，另一个(相同大小)数组包含View中的资源ID，用于显示相应列的数据值。
         * 注意： SimpleCursorAdapter适配器要求游标中必须含有_id这个属性，否则会报错
         */
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview, cursor, from, to, 0);
        ListView listView = findViewById(R.id.list);
        listView.setAdapter(adapter);
    }
    /**
     * 对NewStudent Activity中获得的数据操作，添加数据到数据库中。
     * @param reqCode
     * @param resultCode
     * @param intent
     */
    @Override
    protected  void onActivityResult(int reqCode,int resultCode,Intent intent){
        super.onActivityResult(reqCode,resultCode,intent);
        if (resultCode==RESULT_OK){
            String xm = intent.getStringExtra("xm");
            String xb = intent.getStringExtra("xb");
            String xh = intent.getStringExtra("xh");
            String bj = intent.getStringExtra("bj");
            String zy = intent.getStringExtra("zy");
            String cj = intent.getStringExtra("cj");
            ContentValues values = new ContentValues();
            values.put("xm",xm);
            values.put("xb",xb);
            values.put("xh",xh);
            values.put("bj",bj);
            values.put("zy",zy);
            values.put("cj",cj);

            if (reqCode==1)//请求码1为插入
                dbHelper.insert("Student",values);
            else if (reqCode==2){//请求码2为修改
                String id = intent.getStringExtra("_id");
                dbHelper.updateStudent(id,values);
            }
            renderListView();
        }
    }
    public void setlistener(){
        //为添加按钮设置监听
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //从newstudent Activity中获取数据
                Intent intent=new Intent(MainActivity.this,NewStudent.class);
                startActivityForResult(intent,1);
            }
        });
        //为查询设置监听
        btn_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Find.class);
                startActivity(intent);
            }
        });
        //为listview添加监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            @SuppressLint("Range")
            public void onItemClick(AdapterView<?> parent, View view, int position, long idi) {
                Cursor cursor = dbHelper.queryAll("Student");
                cursor.move(position+1);
                String id = cursor.getString(cursor.getColumnIndex("_id"));
                String xm = cursor.getString(cursor.getColumnIndex("xm"));
                String xb = cursor.getString(cursor.getColumnIndex("xb"));
                String xh = cursor.getString(cursor.getColumnIndex("xh"));
                String bj = cursor.getString(cursor.getColumnIndex("bj"));
                String zy = cursor.getString(cursor.getColumnIndex("zy"));
                String cj = cursor.getString(cursor.getColumnIndex("cj"));
                //向change Activity发送要修改的数据
                Intent intent = new Intent(MainActivity.this,Change.class);
                intent.putExtra("id",id);
                intent.putExtra("xm",xm);
                intent.putExtra("xb",xb);
                intent.putExtra("xh",xh);
                intent.putExtra("bj",bj);
                intent.putExtra("zy",zy);
                intent.putExtra("cj",cj);
                startActivityForResult(intent,2);
            }
        });
        //长按双击删除
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            @SuppressLint("Range")
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long idi) {
                Cursor cursor = dbHelper.queryAll("Student");
                cursor.move(position+1);
                String id = cursor.getString(cursor.getColumnIndex("_id"));//getColumnIndex("_id")得到这一列
                String xm = cursor.getString(cursor.getColumnIndex("xm"));
                builder.setTitle("删除确认").setMessage("是否确认删除学生"+xm);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dbHelper.delStudent(id);
                        renderListView();
                    }
                });
                builder.show();
                return true;
            }
        });
    }
}